package com.zhava.crawler.infrastructure.mapper;

import com.zhava.crawler.client.request.CrawlerRequest;
import com.zhava.crawler.client.request.PaginatedCrawlerRequest;
import com.zhava.crawler.client.response.CrawlerResponse;
import com.zhava.crawler.client.response.PaginatedCrawlerResponse;
import com.zhava.crawler.domain.model.Crawler;
import com.zhava.crawler.domain.model.PaginatedCrawler;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.factory.Mappers;

/**
 * 爬虫对象转换映射器
 *
 * @author zhaxiang
 */
@Mapper
public interface CrawlerMapper {
    
    CrawlerMapper INSTANCE = Mappers.getMapper(CrawlerMapper.class);
    
    /**
     * 将请求对象转换为领域实体
     *
     * @param request 爬虫请求
     * @return 爬虫领域实体
     */
    @Mapping(target = "url", source = "sourceUrl")
    @Mapping(target = "outputFormat", expression = "java(com.zhava.crawler.domain.enums.OutputFormatEnum.fromString(request.getOutputFormat()))")
    @Mapping(target = "rawContent", ignore = true)
    @Mapping(target = "statusCode", ignore = true)
    @Mapping(target = "extractedData", ignore = true)
    @Mapping(target = "links", ignore = true)
    @Mapping(target = "executionTime", ignore = true)
    @Mapping(target = "metadata", ignore = true)
    @Mapping(target = "errorMessage", ignore = true)
    Crawler requestToCrawler(CrawlerRequest request);
    
    /**
     * 将领域实体转换为响应对象
     *
     * @param crawler 爬虫领域实体
     * @return 爬虫响应
     */
    @Mapping(target = "sourceUrl", source = "url")
    @Mapping(target = "content", source = "rawContent")
    @Mapping(target = "extractedLinks", source = "links")
    @Mapping(target = "crawlTime", source = "executionTime")
    CrawlerResponse crawlerToResponse(Crawler crawler);
    
    /**
     * 将分页式爬虫请求对象转换为领域实体
     *
     * @param request 分页式爬虫请求
     * @return 分页式爬虫领域实体
     */
    @Mapping(target = "url", source = "sourceUrl")
    @Mapping(target = "outputFormat", expression = "java(com.zhava.crawler.domain.enums.OutputFormatEnum.fromString(request.getOutputFormat()))")
    @Mapping(target = "statusCode", ignore = true)
    @Mapping(target = "pagesCollected", ignore = true)
    @Mapping(target = "pageResults", ignore = true)
    @Mapping(target = "totalCrawlTime", ignore = true)
    @Mapping(target = "fileExported", ignore = true)
    @Mapping(target = "exportedFilePath", ignore = true)
    @Mapping(target = "errorMessage", ignore = true)
    PaginatedCrawler requestToPaginatedCrawler(PaginatedCrawlerRequest request);
    
    /**
     * 将分页式爬虫领域实体转换为响应对象
     *
     * @param crawler 分页式爬虫领域实体
     * @return 分页式爬虫响应
     */
    @Mapping(target = "sourceUrl", source = "url")
    PaginatedCrawlerResponse paginatedCrawlerToResponse(PaginatedCrawler crawler);
    
    /**
     * 将分页式爬虫的单页结果转换为响应对象中的单页结果
     * 
     * @param pageResult 分页式爬虫的单页结果
     * @return 响应对象中的单页结果
     */
    PaginatedCrawlerResponse.PageResult pageResultToResponsePageResult(PaginatedCrawler.PageResult pageResult);
} 